package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.AuthorInfo;
import org.news.web.constant.CreatorsCH;
import org.news.web.constant.CreatorsConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.CreatorsVo;
import org.news.web.domain.bo.CreatorsBo;
import org.news.web.service.ICreatorsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作者
 *
 * @author Gu Ci
 * @date 2024-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/creators")
public class CreatorsController extends BaseController {

    private final ICreatorsService creatorsService;

    /**
     * 查询作者列表
     */
    @SaCheckPermission("web:creators:list")
    @GetMapping("/list")
    public TableDataInfo<CreatorsVo> list(CreatorsBo bo, PageQuery pageQuery) {
        return creatorsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出作者列表
     */
    @SaCheckPermission("web:creators:export")
    @Log(title = "作者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CreatorsBo bo, HttpServletResponse response) {
        List<CreatorsVo> list = creatorsService.queryList(bo);
        ExcelUtil.exportExcel(list, "作者", CreatorsVo.class, response);
    }

    /**
     * 获取作者详细信息
     *
     * @param creatorId 主键
     */
    @SaCheckPermission("web:creators:query")
    @GetMapping("/{creatorId}")
    public R<CreatorsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long creatorId) {
        return R.ok(creatorsService.queryById(creatorId));
    }

    /**
     * 新增作者
     */
    @SaCheckPermission("web:creators:add")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CreatorsBo bo) {
        return toAjax(creatorsService.insertByBo(bo));
    }

    /**
     * 修改作者
     */
    @SaCheckPermission("web:creators:edit")
    @Log(title = "作者", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CreatorsBo bo) {
        return toAjax(creatorsService.updateByBo(bo));
    }

    /**
     * 删除作者
     *
     * @param creatorIds 主键串
     */
    @SaCheckPermission("web:creators:remove")
    @Log(title = "作者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{creatorIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] creatorIds) {
        return toAjax(creatorsService.deleteWithValidByIds(List.of(creatorIds), true));
    }

    /**
     * 获取称号
     */
    @GetMapping("/getCreatorCH")
    public R<List<CreatorsCH>> getCreatorCH(){
        return R.ok(CreatorsConstant.CHS());
    }


    /**
     * 模糊查询作者
     */
    @GetMapping("/getListCreators")
    public TableDataInfo<CreatorsVo> getListCreators(AuthorInfo bo, PageQuery pageQuery) {
        return creatorsService.getListCreators(bo, pageQuery);
    }
}
